package com.xcity.game.worldboss;

import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.Combat;
import com.xcity.game.combat.CombatTeam;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.worldboss.model.WorldBoss;
import com.xcity.game.worldboss.template.WorldBossConfig;

import naga.x.game.time.TimeUpdater;

public class WorldBossUtils {
	
	static WorldBossService service;
	
	/** 获取剩余挑战次数 */
	public static int getLeftTimes(Player player) {
		int n = player.getPool().getInt(PlayerPool.PROPERTY_WORLD_BOSS_LEFT_TIMES, WorldBossConfig.joinTimesLimit);
		if (n >= WorldBossConfig.joinTimesLimit) {
			return n;
		}
		long time = player.getPool().getLong(PlayerPool.PROPERTY_WORLD_BOSS_LAST_RECOVERY_JOIN_TIMES_TIME, TimeUpdater.getInstance().now());
		time = TimeUpdater.getInstance().now() - time; // elapse millis
		int coolingTimeMillis = WorldBossConfig.joinTimesRecoveryCooldownTime * 1000; // to millis
		if (time > coolingTimeMillis) {
			n += time / coolingTimeMillis;
			if (n < WorldBossConfig.joinTimesLimit) {
				time = TimeUpdater.getInstance().now() - time % coolingTimeMillis; // fix the time
				player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_LAST_RECOVERY_JOIN_TIMES_TIME, time);
			} else {
				n = WorldBossConfig.joinTimesLimit;
				player.poolRemove(PlayerPool.PROPERTY_WORLD_BOSS_LAST_RECOVERY_JOIN_TIMES_TIME);
			}
			player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_LEFT_TIMES, n);
		}
		return n;
	}
	
	/** 获取挑战次数恢复cd（秒） */
	public static int getJoinTimesCooldownTime(Player player) {
		int n = getLeftTimes(player);
		if (n >= WorldBossConfig.joinTimesLimit) {
			return 0;
		}
		long time = player.getPool().getLong(PlayerPool.PROPERTY_WORLD_BOSS_LAST_RECOVERY_JOIN_TIMES_TIME, TimeUpdater.getInstance().now());
		time = time + WorldBossConfig.joinTimesRecoveryCooldownTime * 1000 - TimeUpdater.getInstance().now();
		return (int) (time > 0 ? time / 1000 : 0);
	}
	
	/** 进入战斗前扣次数 */
	public static boolean tryJoin(Player player) {
		int n = getLeftTimes(player);
		if (n < 1) {
			return false;
		}
		int n2 = n - 1;
		player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_LEFT_TIMES, n2);
		if (n >= WorldBossConfig.joinTimesLimit && n2 < WorldBossConfig.joinTimesLimit) {
			// 若本次挑战之前剩余次数已满，则需要重置cd
			player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_LAST_RECOVERY_JOIN_TIMES_TIME, TimeUpdater.getInstance().now());
		}
		return true;
	}
	
	/** 获取挑战积分 */
	public static int getScore(Player player) {
		if (player.getPool().getIntValue(PlayerPool.PROPERTY_WORLD_BOSS_LAST_JOIN_DAY) != TimeUpdater.getInstance().today()) {
			return 0;
		}
		return player.getPool().getIntValue(PlayerPool.PROPERTY_WORLD_BOSS_SCORE);
	}
	
	/** 加积分 */
	public static int addScore(Player player, int val) {
		int score = player.getPool().getIntValue(PlayerPool.PROPERTY_WORLD_BOSS_SCORE);
		if (player.getPool().getIntValue(PlayerPool.PROPERTY_WORLD_BOSS_LAST_JOIN_DAY) != TimeUpdater.getInstance().today()) {
			score = 0;
			player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_LAST_JOIN_DAY, TimeUpdater.getInstance().today());
		}
		score += val;
		player.poolSet(PlayerPool.PROPERTY_WORLD_BOSS_SCORE, score);
		return score;
	}
	
	/** 获取当前boss对象 */
	public static WorldBoss getBoss() {
		return service.getBoss();
	}
	
	/** 计算玩家在本次战斗中输出的总伤害值 */
	public static int calcDamage(Combat combat) {
		int value = 0;
		CombatTeam playerTeam = combat.getSource();
		List<Round> rounds = combat.getRounds();
//		StringBuilder LOG = new StringBuilder(1024);
//		LOG.append(playerTeam.getLeader().getName()).append("\n");
		for (Round r : rounds) {
			List<Action> actions = r.actionList();
//			LOG.append(JSON.toJSONString(actions)).append("\n");
			for (Action action : actions) {
				if (action.unit.getTeam() != playerTeam) {
					continue;
				}
				List<ActionEffect> effects = action.getEffects();
				for (ActionEffect eff : effects) {
					int delta = eff.getHpDelta();
					if (delta < 0) {
						value += -delta;
					}
				}
			}
		}
//		// temp log
//		ConfigEx config = App.getApp().getConfig();
//		File file = new File(config.localDbDir, "__wb_combat__/" + value + "_" + playerTeam.getLeader().getId() + "_" + System.currentTimeMillis());
//		IOUtils.write(file, LOG.toString(), IOUtils.UTF8);
		return value;
	}
	
	/** 计算boss受到的实际伤害值 */
	public static int[] calcNpcHpDelta(Combat combat, int[] npcInitHps) {
		List<CombatUnit> npcs = combat.getTarget().all();
		for (int i = 0; i < npcs.size(); i++) {
			CombatUnit npc = npcs.get(i);
			npcInitHps[i] -= npc.getHp();
		}
		return npcInitHps;
	}

}
